#include<bits/stdc++.h>
using std::cin;
using std::cout;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using pii = std::pair<int, int>;
#define TESTS int _; std::cin >> _; while(_--)
#define all(a) a.begin(), a.end()
#define all1(a) a.begin() + 1, a.end()
#define pb push_back
#define fi first
#define se second
#define SUM(v) std::accumulate(all(v), 0LL)
#define SUM1(v) std::accumulate(all1(v), 0LL)
#define MIN(v) *std::min_element(all(v))
#define MIN1(v) *std::min_element(all1(v))
#define MAX(v) *std::max_element(all(v))
#define MAX1(v) *std::max_element(all1(v))
#define UNIQUE(v) std::sort(all(v)), v.erase(std::unique(all(v), v.end()))
#define UNIQUE1(v) std::sort(all1(v)), v.erase(std::unique(all1(v), v.end()))

constexpr i64 inf = 1E18, Md3 = 998244353, Md7 = 1e9 + 7;
constexpr double eps = 1E-9;
// std::mt19937 rng {std::chrono::steady_clock::now().time_since_epoch().count()};

int main() {
	
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
		
	int N, M;
	cin >> N >> M;

	std::vector<std::vector<pii>> adj(N);
	std::vector<i64> ans(N);
	for(int i = 0; i < M; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		u--;
		v--;
		adj[u].push_back({v, w});
		adj[v].push_back({u, -w});
	}

	std::vector<bool> vis(N);


	auto dfs = [&](auto self, int u, int fa) -> void {
		vis[u] = true;
		for(auto [v, w] : adj[u]) {
			if(v == fa || vis[v]) {
				continue;
			}
			ans[v] = ans[u] + w;
			self(self, v, u);
		}
	};

	for(int i = 0; i < N; i++) {
		if(!vis[i]) {
			dfs(dfs, i, -1);
		}
	}


	
	for(int i = 0; i < N; i++) {
		cout << ans[i] << " \n"[i == N - 1];
	}


	return 0;
}	